/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loginRegister;

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author 黄忠
 */
public class RegisterServlet extends HttpServlet {
    public void wrong(){
        String msg="用户名不存在或密码错误，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void right(){
        String msg="注册信息合格，注册成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String userName=new String(request.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String password=new String(request.getParameter("password1").getBytes("ISO-8859-1"),"UTF-8");
        String repassword=new String(request.getParameter("password2").getBytes("ISO-8859-1"),"UTF-8");
        String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String sex=new String(request.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
        String birth=request.getParameter("year")+"-"+request.getParameter("mouth")+"-"+request.getParameter("day");
        String nation=new String(request.getParameter("nation").getBytes("ISO-8859-1"),"UTF-8");
        String edu=new String(request.getParameter("edu").getBytes("ISO-8859-1"),"UTF-8");
        String work=new String(request.getParameter("work").getBytes("ISO-8859-1"),"UTF-8");
        String phone=new String(request.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        String place=new String(request.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
        String email=new String(request.getParameter("email").getBytes("ISO-8859-1"),"UTF-8");
        if(!(password.equals(repassword))){
            wrong();
            response.sendRedirect("http://localhost:8084/ShiYan5/register/register.jsp");
        }
        else{
            LoginBean nn = new LoginBean();
            nn.setUserName(userName);
            nn.setPassword(password);
            if(nn.register()){
                right();
                response.sendRedirect("http://localhost:8084/ShiYan5/login.jsp");
            }
            else {
                wrong();
                response.sendRedirect("http://localhost:8084/ShiYan5/register/register.jsp");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

   
}
